package com.neu.wms.security;

import com.neu.wms.entity.SysUser;
import com.neu.wms.service.UserService;
import com.neu.wms.vo.MenuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author DAIJUNQIANG
 * @version 1.0
 * @description TODO
 * @date 2023/7/3 22:25
 */
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	private UserService userService;




	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

		// 从数据库中获取用户信息
		SysUser sysUser = userService.selectByUsername(username);
		List<MenuVo> menuVos = userService.selectMenuByName(username, 0);
		String[] permission = userService.selectPermissionByName(username);
		String[] role = userService.selectRoleByUsername(username);


		//将具体信息封装到userDetails中并返回
		LoginUser userDetails = new LoginUser();
		userDetails.setUser(sysUser);
		userDetails.setMenuVoList(menuVos);
		userDetails.setPermissions(permission);
		userDetails.setRoles(role);
		return userDetails;

	}
}
